<?php
class Controller_Product_Bind extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '创建';
		if (isset($_GET['handle']) && $_GET['handle'] == 'copy' && $_GET['id'])
		{
			$product_bind_item = Product_Bind_Item::find('product_bind_id=?', $_GET['id'])
				->joinLeft('product_basic', 'name, number', 'product_bind_item.product_basic_id=product_basic.id')
				->asArray()->getAll();
			$this->_view['product_bind_item'] = $product_bind_item;
		}
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '创建（保存）';
		if ($this->_context->isPOST())
		{
			$item_arr = Helper_BSS_Array::getProductBindItemToArray($_POST);
			$result = Product_Bind_Item::checkItemExists($item_arr);
			if ($result['ack'] == SUCCESS)
			{
				$product_bind_id = Product_Bind::find()->getMax('id');
				$number = sprintf("%05d",((isset($product_bind_id) ? $product_bind_id : 0) + 1));
				$product_bind = array(
						'number' => 'B' . $number,
						'name' => trim($_POST['bind_name']),
						'type' => intval($_POST['type']),
						'item' => $item_arr
				);
				$result = Product_Bind::create($product_bind);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('捆绑成功！', '正在返回..', url('Product::Bind/List'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('捆绑失败！', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
			else
			{
				return $this->_redirectMessage('捆绑失败！', $result['message'], $result['url'], $result['ack']);
			}
		}
	}
	
	function actionBindEdit()
	{
		$this->_view['title'] = '编辑';
		$product_bind = Product_Bind::find('id=?', $_GET['id'])->asArray()->getOne();
		if ($product_bind['id'])
		{
			$product_bind['item'] = Product_Bind_Item::find('product_bind_item.product_bind_id=?', $product_bind['id'])
				->joinLeft('product_basic', 'name, number', 'product_basic.id=product_bind_item.product_basic_id')
				->asArray()->getAll();
			$this->_view['product_bind'] = $product_bind;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '该捆绑不存在！', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionBindEditSave()
	{
		$this->_view['title'] = '编辑（保存）';
		if ($this->_context->isPOST())
		{
			$product_bind = Product_Bind::find('id=?', $_POST['product_bind_id'])->asArray()->getOne();
			if ($product_bind['id'])
			{
				QDB::getConn()->execute('update product_bind set name=\'' . (empty($_POST['name']) ? '' : $_POST['name']) . '\',type=' . (empty($_POST['type']) ? 1 : $_POST['type']) . ' where id=' . $product_bind['id']);
				return $this->_redirectMessage('保存成功', '正在返回..', url('Product::Bind/List'), SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('保存失败', '该捆绑不存在！', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '产品编辑';
		$product_bind = Product_Bind::find('id=?', $_GET['id'])->asArray()->getOne();
		if ($product_bind['id'])
		{
			$product_bind['item'] = Product_Bind_Item::find('product_bind_item.product_bind_id=?', $product_bind['id'])
				->joinLeft('product_basic', 'name, number', 'product_basic.id=product_bind_item.product_basic_id')
				->asArray()->getAll();
			$this->_view['product_bind'] = $product_bind;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '该捆绑不存在！', $_SERVER['HTTP_REFERER'], FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '产品编辑（保存）';
		if ($this->_context->isPOST())
		{
			$product_bind = Product_Bind::find('id=?', $_POST['product_bind_id'])->asArray()->getOne();
			if ($product_bind['id'])
			{
				$item_arr = Helper_BSS_Array::getProductBindItemToArray($_POST);
				$product_bind['name'] = $_POST['bind_name'];
				$product_bind['type'] = $_POST['type'];
				$product_bind['item'] = $item_arr;
				$result = Product_Bind::edit($product_bind);
				if ($result['ack'] == SUCCESS)
				{
					return $this->_redirectMessage('保存成功', '正在返回..', url('Product::Bind/List'), $result['ack']);
				}
				else
				{
					return $this->_redirectMessage('保存失败', $result['message'], $_SERVER['HTTP_REFERER'], $result['ack']);
				}
			}
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 10;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = Product_Bind::search($url_arr, $cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$product_bind = $result['data'];
			foreach ($product_bind as &$pb)
			{
				$warehouse_id = Warehouse::getDefaultWarehouseId();
				$item = Product_Bind_Item::find('product_bind_item.product_bind_id=? and product_warehouse.warehouse_id=?', $pb['id'], $warehouse_id)
					->joinLeft('product_basic', 'number, name, purchase_price, last_purchase_price, weight', 'product_basic.id=product_bind_item.product_basic_id')
					->joinLeft('product_warehouse', 'purchase_status, sales_status, actual_quantity, weekly_sales, monthly_sales', 'product_warehouse.product_basic_id=product_basic.id')
					->asArray()->getAll();
				Helper_BSS_Normal::getProductImgUrl($item);
				$pb['item'] = $item;
			}
			unset($pb);
			$page = new Helper_BSS_Page($result['pagination'], url('Product::Bind/List', $url_arr));
			$this->_view['page'] = $page->getPage();
			$this->_view['product_bind'] = $product_bind;
		}
	}
	
	function actionProductInfoAjax()
	{
		$this->_view['title'] = '产品信息请求ajax';
		$product_basic = Product_Basic::search(array('number_or_name' => $_REQUEST['q']), 0, 15);
		$result = array();
		foreach ($product_basic['data'] as $pb)
		{
			$result[] = array(
					0 => $pb['number'] . ' ' . $pb['name'],
					'id' => $pb['id'],
					'number' => $pb['number'],
					'purchase_price' => $pb['purchase_price'],
					'cost_price' => $pb['last_purchase_price'],
					'preferred_provider_id' => $pb['preferred_provider_id'],
					'preferred_provider_name' => $pb['preferred_provider_name'],
					'weekly_sales' => $pb['weekly_sales'],
					'monthly_sales' => $pb['monthly_sales'],
			);
		}
		echo json_encode($result);
		exit;
	}
}